
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % INITIALIZE PROGRAMME
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear;
set_parallel=1;
initialize_main;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % CONFIGURE SETTINGS
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

outline=prepare_outline;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % START LOOP:
    % 
    %  - z=1,2,3: UNCONSTRAINED
    %  - z=4,5,6: CONSTRAINED
    %  - z=7: UNCONSTRAINED MEAN
    %  - z=8: CONSTRAINED MEAN
    %  - z=9,10,11: LINEAR QUANTILE REGRESSION
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

for zz=1:11, 

        tau=outline(zz).tau;
           
        fprintf('\n------------------------------------------------------------------');
        fprintf(['\nzz=%3.0f, tau=%5.3f: ' outline(zz).run_name '. ' datestr(clock)],zz,tau);
        fprintf('\n------------------------------------------------------------------');
                    
        config_id=outline(zz).config_id;     
        run_name=outline(zz).run_name;

        input.tau=tau;
        input.n1=outline(zz).n1;
        input.n2=outline(zz).n2;
        
        input.k1=outline(zz).k1;
        input.k2=outline(zz).k2;
        input.CI=outline(zz).CI;
        input.reps=outline(zz).reps;
                                          
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % INITIALIZE DATA AND ROUTINE
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        data=initialize_data;
        config=initialize_config(data, input,config_id);
        
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % RUN ROUTINE
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        estimates=estimate_wrapper(config_id,config, input);

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % PREPARE OUTPUT
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%
            
        results=compute_results(config,estimates);

            %%%%%%%%%%%%%%%%
            % OUTPUT
            %%%%%%%%%%%%%%%%
        
        output_name=['./output_' run_name '_config' num2str(config_id) '.mat'];
        clock_save=clock;
        fprintf(['... saving file: ' output_name '. Timestamp: ' datestr(clock_save) '.\n']);
        
        y_interest=data.y_interest;
        p_interest=data.p_interest;
        y_interest_univ=data.y_interest_univ;
        
        qhat=results.qhat;

        CI_logQ_low=NaN*p_interest*ones(1,3);
        CI_logQ_high=NaN*p_interest*ones(1,3);

		if config.CI==1,
			CI_logQ_low=results.CI_logQ_low;
			CI_logQ_high=results.CI_logQ_high;
		end
    
		DWL=results.DWL;
		DWL_pertax=results.DWL_pertax;
		DWL_perinc=results.DWL_perinc;
     
        save(output_name,'estimates','config_id','input','y_interest_univ','p_interest','y_interest','*CI*','qhat','*DWL*'); 
            
end;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % FINALIZE
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%

finalize_main;

    %
    
